<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn"
        slot="title"
        type="info"

        size="small"
        round
        icon="search"
        to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- tab 标签 -->
    <!-- v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签。 -->
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs"
      v-model="active"
      animated
      swipeable>
      <van-tab v-for="item in channel"
        :key="item.id"
        :title="item.name">
        <!-- 文章列表组件 -->
        <Article :channel="item"></Article>
      </van-tab>

      <div slot="nav-right"
        class="placeholder"></div>
        <!-- 汉堡按钮 -->
      <div slot="nav-right"
        class="gengduo-btn" @click="channelEditShow = true">
        <i slot="icon"
          class="toutiao toutiao-gengduo"></i>
      </div>

    </van-tabs>

     <!-- 频道编辑弹出层 -->
      <van-popup v-model="channelEditShow"
        closeable
        position="bottom"
        close-icon-position="top-left"
        :style="{ height: '100%' }">

        <!-- 频道编辑 -->
        <ChannelEdit :active="active" :myChannels="channel"
        @updata-active="onUpdataActive"></ChannelEdit>
      </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import Article from './components/ArticleList.vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/index.js'

export default {
  name: 'Home',
  data () {
    return {
      active: 0,
      // 存储用户频道
      channel: [],
      // 控制频道编辑弹出层
      channelEditShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    // 获取频道列表
    this.getUserList()
  },
  components: {
    Article,
    ChannelEdit
  },
  methods: {
    // 获取频道列表
    async getUserList () {
      try {
        // const { data } = await getUserChannels()
        // this.channels = data.data.channels
        // let channels = []
        if (this.user) {
          // 已登录，请求获取用户频道列表
          const { data: res } = await getUserChannels()
          this.channel = res.data.channels
        } else {
          // 未登录，判断是否有本地的频道列表数据
          const localChannels = getItem('channels')
          //    有，拿来使用
          if (localChannels) {
            this.channel = localChannels
          } else {
            //    没有，请求获取默认频道列表
            const { data: res } = await getUserChannels()
            this.channel = res.data.channels
          }
        }

        // this.channel = channels
      } catch (err) {
        this.$toast('获取频道数据失败')
      }
    },

    // 切换到频道主页后操作
    onUpdataActive (index, channelEditShow) {
      // 更新激活的频道项
      this.active = index
      // 关闭编辑弹层

      this.channelEditShow = channelEditShow
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
 padding-top: 174px;
  padding-bottom: 100px;
 /deep/ .van-nav-bar__title {
    max-width:unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;

    .van-icon {
      font-size: 32px;
      color: #fff;
    }
  }

// tab标签样式
/deep/ .channel-tabs{
 .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab{
      min-width: 200px;
      height: 83px;
      border-right: 1px solid #edeff3;
      font-size: 28px;
    }
    .van-tabs__nav--line {
      padding-bottom: 0;
    }
    .van-tabs__line{
      bottom: 8px;
      width: 31px;
      height: 6px;
      background-color: #3296fa;
    }
  }

  // gengduo-btn 样式
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  .gengduo-btn{
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 86px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao{
      font-size: 33px;
    }
    &:before { // &:before 相当于 .gengduo-btn:before
    content: "";
    position: absolute;
    left: 0;
    width: 2px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
  }

}
</style>
